package com.sinoglobal.wallet.activity;

import com.sinoglobal.wallet.R;
import com.sinoglobal.wallet.api.RemoteImpl;
import com.sinoglobal.wallet.app.SinoConstans;
import com.sinoglobal.wallet.app.SinoValueManager;
import com.sinoglobal.wallet.entity.W_RsVo;
import com.sinoglobal.wallet.task.MyAsyncTask;
import com.sinoglobal.wallet.util.NetWorkUtil;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
/**
 *
 * @author lijingjing
 * @createdate 2015-10-22 上午11:38:04
 * @Description: TODO(支付密码)
 */
public class W_PayPasswordActivity extends W_SinoBaseActivity {
	private TextView pay_passWord_Sure, payPassWord_code, phoneCallTask;//确定按钮  验证码  拨打电话
	private CountDownTimer timer; // 倒计时类
	private EditText payPassword_edit;
	private String title_flag;
	private String code;
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.wallet_pay_password_activity);
		init();
	}
	private void init(){
		title_flag = getIntent().getStringExtra("title_flag");
		if(title_flag!=null){
			if(title_flag.toString().equals("3")){
				mTemplateTitleText.setText("支付密码");
			}else if(title_flag.toString().equals("4")){
				mTemplateTitleText.setText("找回支付密码");
			}}
		pay_passWord_Sure = (TextView) findViewById(R.id.pay_passWord_Sure);
		payPassWord_code = (TextView) findViewById(R.id.payPassWord_code);
		payPassword_edit = (EditText) findViewById(R.id.payPassword_edit);
		phoneCallTask = (TextView) findViewById(R.id.PhoneCallTask);
	//	String phoneNum = (String) SinoValueManager.getValue(this, SinoConstans.KEY_Phone,null);
		String phoneNum = SinoValueManager.getInstance().getUserPhone(this);
		if(phoneNum!=null&&phoneNum.length()>7){
			String maskNumber = phoneNum.substring(0,3)+"****"+phoneNum.substring(7,phoneNum.length());
			phoneCallTask.setText("验证码将发送到注册账号时的手机号" + maskNumber + "， 请注意查收如有问题请拨打400-832-1933");}
		phoneCallTask.setOnClickListener(this);
		pay_passWord_Sure.setOnClickListener(this);
		payPassWord_code.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if(id == R.id.pay_passWord_Sure){
			Bundle bund = new Bundle();
			bund.putString("flag", "2");
			if(payPassword_edit.getText().toString().equals("")){
				showToast("验证码不能为空");
			}else if(!payPassword_edit.getText().toString().equals(code)){
				showToast("验证码输入有误");
			}else{
				showToast(payPassWord_code.getText().toString());
				if(payPassWord_code.getText().toString().equals("获取验证码")){
					showToast("超时，请重新获取验证码");
				}else{
					verifyCode();
					bund.putString("tit", title_flag);
					goIntent(W_PayPasswordSureOK.class, bund);
					finish();
				}
			}
		}else if(id == R.id.payPassWord_code){
			if(getNetWork(W_PayPasswordActivity.this)) {
				showToast("网络连接失败");
			}else{
				showToast("验证码已发送");
				toValidateCode();
				getCode();//验证码
			}
		}else if(id == R.id.PhoneCallTask){//拨打电话
			showDialog(W_PayPasswordActivity.this, "提示", "您确定拨打电话吗？", null ,R.color.w_c14, null,R.color.w_c14,true,new DialogOnClickListener() {

				@Override
				public void onSure(View v) {
					Intent intent = new Intent(Intent.ACTION_DIAL);
					intent.setData(Uri.parse("tel:" + "4008321933"));
					startActivity(intent);
				}

				@Override
				public void onCancle(View v) {

					//					showToast("取消");
				}
			});

		}else if (id == R.id.wallet_noData) {
			 if(NetWorkUtil.getNetWorkInfoType(getApplicationContext())==NetWorkUtil.NONE){
					showToast("请检查网络是否连接！");	
					 loadError(true);
					}else{
						loadError(false);;
					}
		}
	}

	private void toValidateCode() {
		payPassWord_code
				.setBackgroundResource(R.drawable.w_pop_bg_selector_disabled);
		payPassWord_code.setTextColor(getResources().getColor(R.color.w_gray));
		updateSendCodeTriggerState(false);
		startTimer();
	}
	private void updateSendCodeTriggerState(final boolean enabled) {
		// 是否可被点击
		payPassWord_code.setEnabled(enabled);
		if (enabled) {
			payPassWord_code.setText("获取验证码");
			payPassWord_code
					.setBackgroundResource(R.drawable.w_pop_bg_selector_nomal);
			payPassWord_code.setTextColor(getResources().getColor(R.color.w_c1));
		}
	}

	private void startTimer() {
		// 第一个参数 倒计时总时间 例60000=60s 第二个参数倒计时参数 1000 =1s
		timer = new CountDownTimer(60000, 1000) {
			@Override
			public void onTick(long millisUntilFinished) {
				updateTimerUI(millisUntilFinished);
			}
			@Override
			public void onFinish() {
				updateSendCodeTriggerState(true);
			}
		};
		timer.start();
	}
	private void updateTimerUI(final long millisUntilFinished) {
		payPassWord_code.setText(String.format(" %s 秒后重获",
				millisUntilFinished / 1000));
	}
	//获取手机验证码接口
	private void getCode(){
		MyAsyncTask<W_RsVo> task = new MyAsyncTask<W_RsVo>(true,this,true) {
			@Override
			public W_RsVo before(Void... params) throws Exception {
				// TODO 测试接口
				return RemoteImpl.getInstance().getCode(W_PayPasswordActivity.this);
			}
			@Override
			public void after(W_RsVo result) {

				if (result.getCode().equals("100")) {
					if(result != null){
						code = result.getRs().toString();
//						payPassword_edit.setText(code);
					}
				} else {
					showToast(result.getMsg());
				}

			}

			@Override
			public void exception() {
			}
		};
		task.execute();

	}
	//验证手机验证码接口
	private void verifyCode(){

		MyAsyncTask<W_RsVo> task = new MyAsyncTask<W_RsVo>(true,this,true) {



			@Override
			public W_RsVo before(Void... params) throws Exception {
				// TODO 测试接口
				return RemoteImpl.getInstance().verifyCode(W_PayPasswordActivity.this, payPassword_edit.getText().toString());
			}

			@Override
			public void after(W_RsVo result) {

				if (result.getCode().equals("100")) {
					if(result != null){
						showToast(result.getRs());
					}
				} else {
					showToast(result.getMsg());
				}
			}

			@Override
			public void exception() {
			}
		};
		task.execute();

	}
}
